import 'dart:typed_data';

import 'package:bluetooth_protocols_plugin/src/base/integrated/integrated_production_info_base.dart';
import 'package:bluetooth_protocols_plugin/src/tools/utils.dart';

// 综合产品信息
abstract class LorinIntegratedProductionInfo
    extends IntegratedProductionInfoBase {
  // 数据结构格式，从1开始
  int dataFormat = 1;

  // 接口类型
  int interfaceType = 0;

  // 车辆sn
  String sn = '';

  // 主系列
  int mainSeries = 0;

  // 子系列
  int subSeries = 0;

  // 具体型号
  int model = 0;

  // 特征
  int feature = 0;

  // 产品型号
  String productModel = '';

  // 固件信息
  List<Map<String, dynamic>> firmwareInfo = [];

  // 激活日期
  DateTime activationDate = DateTime.now();

  LorinIntegratedProductionInfo.fromBytes(Uint8List bytes)
      : super.fromBytes(bytes) {
    if (bytes.length < minLength) {
      throw ArgumentError(
          '字节数组长度不足，无法解析 $runtimeType，期望长度至少为 $minLength，实际长度为 ${bytes.length}');
    }
    var byteData = ByteData.sublistView(bytes);
    dataFormat = byteData.getInt16(0, Endian.little);
    interfaceType = byteData.getInt8(2);
    sn = Utils.uint8List2Text(bytes.sublist(3, 24));
    print("bytes sn= ${bytes.sublist(3, 24)}, sn = $sn");
    mainSeries = byteData.getInt8(24);
    subSeries = byteData.getInt8(25);
    model = byteData.getInt8(26);
    feature = byteData.getInt8(27);
    productModel = Utils.uint8ListToHexString(
        Uint8List.fromList([mainSeries, subSeries, model, feature]));
  }

  @override
  int dataIntValue() {
    throw UnimplementedError();
  }

  @override
  int dataLength() {
    throw UnimplementedError();
  }

  @override
  int get minLength => 28;

  @override
  Uint8List? toBytes() {
    throw UnimplementedError();
  }

  @override
  Map<String, dynamic> toMap() {
    return {
      'dataFormat': dataFormat,
      'interfaceType': interfaceType,
      'sn': sn,
      'mainSeries': mainSeries,
      'subSeries': subSeries,
      'model': model,
      'feature': feature,
      'productModel': productModel,
    };
  }
}
